package com.huitone.gddw.service.impl.worktable;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.dao.entity.PrgExtInfo;
import com.huitone.gddw.dao.entity.PrgInfo;
import com.huitone.gddw.dao.worktable.IPrgInfoDao;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.IPrgExtInfoService;
import com.huitone.gddw.service.IPrgInfoService;
import com.huitone.gddw.service.dto.PrgInfoDTO;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * @Title PrgInfoService
 * @Package com.huitone.gddw.service.impl
 * @Description TODO
 * @author chengsiyue
 * @date 2017年4月21日 下午5:53:19
 * @version V1.0
 */
@Service
public class PrgInfoService implements IPrgInfoService {
	@Autowired
	private IPrgInfoDao prgInfoDao;
	@Autowired
	private IPrgExtInfoService prgExtInfoService;

	@Override
	public int deleteByPrimaryKey(String prgId) {
		return prgInfoDao.deleteByPrimaryKey(prgId);
	}

	@Override
	public PrgInfo selectByPrimaryKey(String prgId) {
		return prgInfoDao.selectByPrimaryKey(prgId);
	}


	@Override
	public List<PrgInfo> selectPrgInfoByMap(Map<String, Object> map) {
		return prgInfoDao.selectPrgInfoByMap(map);
	}

	/**
	 * 列表显示程序运行信息
	 * @param qp
	 * @return
	 */
	@Override
	public QueryPage<PrgInfoDTO> selectPage(QueryPage<PrgInfo> qp) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("prgName", qp.getParams().get("prgName"));
		map.put("deployId", qp.getParams().get("deployId"));
		map.put("start", (qp.getPageNo() - 1) * qp.getPageSize()+1);
		map.put("end", (qp.getPageNo() - 1) * qp.getPageSize() + qp.getPageSize());
		List<PrgInfoDTO> result = prgInfoDao.selectPage(map);

		int totalCount = prgInfoDao.selectCount(map);
		QueryPage<PrgInfoDTO> queryPage = new QueryPage<>();
		queryPage.setResult(result);
		queryPage.setPageNo(qp.getPageNo());
		queryPage.setPageSize(qp.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}

	@Override
	public void save(PrgInfo record, List<PrgExtInfo> listSaveExt) {
		record.setInsertStaffId(SysLoginUtil.getLoginUserId());
		record.setInsertTime(new Date());
		prgInfoDao.save(record);
		for (PrgExtInfo prgExtInfo : listSaveExt) {
			prgExtInfo.setInsertStaffId(SysLoginUtil.getLoginUserId());
			prgExtInfo.setInsertTime(new Date());

			prgExtInfoService.save(prgExtInfo);
		}
	}

	/**
	 * 修改页面 包括操作：增、删、改
	 * @param record
	 * @param listSaveExt
	 * @param listUpdateExt
	 * @param listDelExt
	 */
	@Override
	public void update(PrgInfo record, List<PrgExtInfo> listSaveExt, List<PrgExtInfo> listUpdateExt,List<PrgExtInfo> listDelExt) {
		record.setLastModStaffId(SysLoginUtil.getLoginUserId());
		record.setLastModTime(new Date());
		prgInfoDao.updateByPrimaryKey(record);
		for (PrgExtInfo prgExtInfo : listSaveExt) {
			prgExtInfo.setLastModStaffId(SysLoginUtil.getLoginUserId());
			prgExtInfo.setLastModTime(new Date());
			prgExtInfoService.save(prgExtInfo);
		}
		for (PrgExtInfo prgExtInfo : listUpdateExt) {
			prgExtInfo.setLastModStaffId(SysLoginUtil.getLoginUserId());
			prgExtInfo.setLastModTime(new Date());

			prgExtInfoService.updateByPrimaryKey(prgExtInfo);

		}
		for (PrgExtInfo prgExtInfo : listDelExt) {
			prgExtInfoService.deleteById(prgExtInfo.getRecId());
		}
	}

	/**
	 * 逻辑删除 
	 * @param prgId
	 * @return
	 */
	@Override
	public int delete(String prgId) {
		
		return prgInfoDao.updateStatus(prgId);
	}
	

	/**
	 * “修改”页面 信息显示
	 * @param prgId
	 * @return
	 */
	@Override
	public PrgInfoDTO toUpadte(String prgId){
		PrgInfo prgInfoForUpdate = prgInfoDao.selectByPrimaryKey(prgId);
		List<PrgExtInfo> prgExtInfoListForUpdate = prgExtInfoService.selectByPrgId(prgId);
		PrgInfoDTO prgInfoDTO = new PrgInfoDTO(prgInfoForUpdate,prgExtInfoListForUpdate);
		return prgInfoDTO;
	}

	@Override
	public int updateStatus(String prgId) {
		return prgInfoDao.updateStatus(prgId);
	}

	@Override
	public List<PrgInfoDTO> getRecord(Map<String, Object> map) {
		
		return  prgInfoDao.getRecord(map);
	}

	@Override
	public List<PrgInfo> getByParamtabId(String paramtabId) {
		return prgInfoDao.getByParamtabId(paramtabId);
	}
}
